﻿using Dyao.Book.Application;
using Dyao.Book.Repository.Shared;
using Foundation.AutoMapper;
using Foundation.Caching;
using Foundation.EntityFrameworkCore;
using Foundation.EntityFrameworkCore.SqlServer;
using Foundation.IdCreate;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Dyao.Bus.Extensions.Configuration
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// Db Context Extensions
        /// </summary>
        /// <param name="services">IOC</param>
        /// <param name="configuration">配置对象</param>
        public static void AddDbContext(this IServiceCollection services, IConfiguration configuration)
        {
            services.TryAddTransient<DataDbContext>();
            services.AddFoundationDbContext<DataDbContext>(options =>
            {
                options.AddDefaultRepositories(true);
            });

            services.Configure<FoundationEntityOptions>(options => { });

            services.Configure<DbConnectionOptions>(options =>
            {
                options.ConnectionStrings.Add("DataConn", configuration.GetConnectionString("DataConn"));
                options.ConnectionStrings.Add("DataConnRead", configuration.GetConnectionString("DataConnRead"));
            });

            services.Configure<FoundationDbContextOptions>(options =>
            {
                options.UseSqlServer(optionsBuilder =>
                {
                    //optionsBuilder.UseCompatibilityLevel(120); // .NET Core 8 兼容低版本SQLSERVER
                });

                options.UseSqlServer<DataDbContext>(optionsBuilder =>
                {
                    //optionsBuilder.UseCompatibilityLevel(120); // .NET Core 8 兼容低版本SQLSERVER
                });

            });
        }

        /// <summary>
        /// Object Mapping Extensions
        /// </summary>
        /// <param name="services"></param>
        public static void AddObjectMapping(this IServiceCollection services, Action<AutoMapperOptions> configureAction = null)
        {
            services.Configure<AutoMapperOptions>(options =>
            {
                options.AddProfile<ModelMappingProfile>();

                configureAction?.Invoke(options);
            });
        }


        /// <summary>
        /// 添加缓存配置
        /// </summary>
        /// <param name="services"></param>
        public static void AddCustomCaching(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddCollectCaching();

            services.AddEasyCaching(option =>
            {
                // local
                option.UseInMemory(configuration, "InMemoryCache");

                // distributed
                option.UseRedis(configuration, "RedisCache");

                // combine local with bus
                option.UseDistributeInMemory(configuration, "DistributedInMemoryCache");

                // combine local, distributed with bus
                option.UseHybrid(configuration, "DistributedMultiLevelCache");

                // use Redis bus
                option.WithRedisBus(configuration);

                // use json serialization
                option.WithJson("RedisDyao");
            });
        }


        /// <summary>
        /// 添加 id 创建服务
        /// </summary>
        /// <param name="services"></param>
        public static void AddIdCreate(this IServiceCollection services)
        {
            services.AddIdCreateService(createBuilder =>
            {
                createBuilder.AdddStoreConfigure(storeBuilder =>
                {
                    storeBuilder.RegisterConfiguration(typeof(ApplicationModule).Assembly);
                });
            });
        }
    }
}
